/* 十六进制转RGBA */
export function hexToRgba(bgColor, alpha = 0.3) {
  if (!bgColor) {
    return '#fff'
  }
  let color = bgColor.slice(1) // 去掉'#'号
  let rgba = [
    parseInt('0x' + color.slice(0, 2)),
    parseInt('0x' + color.slice(2, 4)),
    parseInt('0x' + color.slice(4, 6)),
    alpha,
  ]
  return 'rgba(' + rgba.toString() + ')'
}
/* 解析获取所有文件集合 */
export const importAll = (r) => {
  let cache = {}
  r.keys().forEach(key => cache[key] = r(key))
  // 生成待待注册组件集合
  const componentsAll = {}
  for (const key in cache) {
    const element = cache[key].default
    componentsAll[element.name] = element
  }
  return componentsAll;
}
// 一维数组转二维数组
export function arrayGroupBy(list, groupId) {
  let sorted = groupBy(list, (item) => {
    return [item[groupId]];
  });
  return sorted;
}
export function groupBy(array, f) {
  let groups = {};

  array.forEach((o) => {
    var group = JSON.stringify(f(o));
    groups[group] = groups[group] || [];
    groups[group].push(o);
  });
  return Object.keys(groups).map((group) => {
    let name = JSON.parse(group)[0]
    return { list: groups[group], name };
  });
}